package com.system.Daos;

import com.system.Models.Admin;

import java.sql.*;

public class AdminDao {

    Admin admin;

    public AdminDao(Admin admin){
        this.admin = admin;
    }
    public boolean vailPassword() throws SQLException {

        boolean flag = false;
        String name = this.admin.getName();
        String pwd = this.admin.getPassword();

        Connection con = new MySQLExample().getCon();

        // 创建Statement对象
        Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);

        String sql = "SELECT * FROM admininfo where name = '"+name+"' and password = '"+pwd+"'";

        // 执行查询并获取结果集
        ResultSet rs = stmt.executeQuery(sql);
        int rowCount = 0;
        rs.last();
        rowCount = rs.getRow();

        if(rowCount == 1){
            flag = true;
        }

        // 关闭连接、语句和结果集
        rs.close();
        stmt.close();
        con.close();

        return flag;

    }

    public boolean addAdmin() throws SQLException {

        boolean flag = false;
        String name = this.admin.getName();
        String pwd = this.admin.getPassword();

        Connection con = new MySQLExample().getCon();

        String sql = "INSERT INTO `adminInfo` (`name`, `password`) VALUES ('"+name+"', '"+pwd+"');";

        // 创建Statement对象
        Statement stmt = con.createStatement();

        int rowsAffected = stmt.executeUpdate(sql);

        if(rowsAffected == 1){
            flag = true;
        }

        // 关闭连接、语句和结果集
        stmt.close();
        con.close();

        return flag;

    }

    public boolean changePassword(String newPwd) throws SQLException {

        boolean f = false;
        // 构造更新语句

        if (!vailPassword()){
            return f;
        }

        Connection conn = new MySQLExample().getCon();

        String sql = "UPDATE admininfo SET password = ? WHERE name = ?";

        // 创建 PreparedStatement 对象，并设置参数
        PreparedStatement statement = conn.prepareStatement(sql);
        statement.setString(1, newPwd);
        statement.setString(2, this.admin.getName());

        // 执行更新操作
        int rowsAffected = statement.executeUpdate();

        if (rowsAffected == 1) {
            f = true;
        }

        // 关闭连接
        statement.close();
        conn.close();

        return f;

    }

    public boolean isExist() throws SQLException{

        boolean flag = false;
        String name = this.admin.getName();

        Connection con = new MySQLExample().getCon();

        // 创建Statement对象
        Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);

        String sql = "SELECT * FROM admininfo where name = '"+name+"'";

        // 执行查询并获取结果集
        ResultSet rs = stmt.executeQuery(sql);
        int rowCount = 0;
        rs.last();
        rowCount = rs.getRow();

        if(rowCount > 0){
            flag = true;
        }

        // 关闭连接、语句和结果集
        rs.close();
        stmt.close();
        con.close();

        return flag;

    }

}
